import { NextRequest, NextResponse } from 'next/server'
import { updateTool } from '@/lib/database'
import { requireAdmin, withErrorHandling } from '@/lib/auth-middleware'

export const POST = withErrorHandling(async (request: NextRequest, context: any) => {
  return requireAdmin(request, async (req) => {
    // 从URL中提取ID
    const url = new URL(request.url)
    const pathSegments = url.pathname.split('/')
    const id = pathSegments[pathSegments.length - 2] // reject前面的ID

    console.log(`管理员 ${req.user?.name} 拒绝工具: ${id}`)

    if (!id) {
      return NextResponse.json(
        { error: '工具ID不能为空' },
        { status: 400 }
      )
    }

    // 更新工具状态为已拒绝
    await updateTool(id, {
      status: 'REJECTED',
      updatedAt: new Date().toISOString()
    })

    return NextResponse.json({
      success: true,
      message: '工具已拒绝'
    })
  })
})
